package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;

import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类接口
     *
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        //业务层的数据模型与数据库数据模型不一致
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.DISABLE);
        //创建时间
//        category.setCreateTime(LocalDateTime.now());
        //更新时间
//        category.setUpdateTime(LocalDateTime.now());
        //创建人
        //从上下文获取到当前用户id
//        Long currentId = BaseContext.getCurrentId();
//        category.setCreateUser(currentId);
        //修改人
//        category.setUpdateUser(currentId);
        categoryMapper.addCategory(category);
    }

    @Override
    public PageResult listPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //放入pageHelper插件
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        PageHelper.startPage(page, pageSize);
        //借助dao层查询
        Page<Category> categories = categoryMapper.listPage(categoryPageQueryDTO);
        long total = categories.getTotal();
        List<Category> result = categories.getResult();
        return new PageResult(total, result);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        //查看是否关联了菜品
        Integer count = dishMapper.queryByid(id);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        // 查看是否关联了套餐
        count = setmealMapper.queryByid(id);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除分类
        categoryMapper.deleteCategory(id);
    }

    /**
     * 启用和禁用功能
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void onOrOff(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.update(category);
    }

    /**
     * 修改分类
     * @param
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //修改人id
//        Long currentId = BaseContext.getCurrentId();
//        category.setUpdateUser(currentId);
        //修改时间
//        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 根据分类查询
     * @param type
     * @return
     */
    @Override
    public List<Category> typeQuery(Integer type) {
        List<Category> list= categoryMapper.typeQuery(type);
        return list;
    }
}
